package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.pojo.Dish;
import com.itheima.reggie.pojo.Setmeal;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 套餐管理
 */
@RestController
@RequestMapping("setmeal")
public class SetmealController {
    @Autowired
    SetmealService setmealService;


    /**
     * 添加套餐
     * @param setmealDto
     * @return
     */
  @PostMapping
  @CacheEvict(value = "setMeal",allEntries = true)
    public R<String> saveSetMealDto(@RequestBody SetmealDto setmealDto){
      setmealService.saveSetMealDto(setmealDto);
      return R.success("添加成功");
  }

    /**
     * 分页套餐查询
     */

    @GetMapping("page")
    public R<Page<SetmealDto>> pageSetMeal(Integer page, Integer pageSize,String name){
        Page<SetmealDto> pageInfo= setmealService.pageSetMeal(page,pageSize,name);
        return R.success(pageInfo);
    }
    /**
     * 启售/停售
     */
    @PostMapping("status/{status}")
    public R<String> deleteByIds(@PathVariable Integer status,Long[] ids){
        setmealService.update().set("status",status)
                .in("id",ids)
                .update();
        return R.success("修改成功");
    }
    /**
     * 删除套餐
     */
    @DeleteMapping
    @CacheEvict(cacheNames = "setMeal",allEntries = true)
    public R<String> deleteByIds(Long[] ids){
        setmealService.deleteByIds(ids);
        return R.success("删除成功");
    }
    /**
     * 根据ID查询数据回显
     */
    @GetMapping("{id}")
    @Cacheable(cacheNames = "setMealDto",key = "#id",unless = "#result==null")
    public R<SetmealDto> getSetMealDto(@PathVariable Long id){
        SetmealDto setmealDto= setmealService.getSetMealDto(id);
        return R.success(setmealDto);
    }
    /**
     * 修改
     */
    @PutMapping
    @CacheEvict(cacheNames = "setMealDto",key = "#setmealDto.getId()")
    public R<String> updateById(@RequestBody SetmealDto setmealDto){
        setmealService.updateByIds(setmealDto);
        return R.success("修改成功");
    }

    /**
     * 查询分类套餐的信息
     * @param categoryId
     * @param status
     * @return
     */
    @GetMapping("list")
    @Cacheable(cacheNames = "setMeal",key = "#categoryId+#status",unless = "#result.data.size()==0")
    public R<List<Setmeal>> listSetMeal(Long categoryId , Integer status){
        List<Setmeal> list = setmealService.query()
                .eq("category_id", categoryId)
                .eq("status", status)
                .orderByAsc("update_time")
                .list();
        return R.success(list);
    }
    /**
     * 移动端查询套餐菜品信息
     */
    @GetMapping("dish/{id}")
    public R<List<DishDto>> setMealDish(@PathVariable Long id){
        List<DishDto> dishList=  setmealService.setMealDish(id);
        return R.success(dishList);
    }
}
